package org.example.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.Dao.CourseDao;
import org.example.Dao.GradeDao;
import org.example.Entity.Course;
import org.example.Entity.Score;
import org.example.Entity.Student;
import org.example.Utils.MybatisUtils;
import java.util.List;
public class GradeController {
    //插入课程接口
    public int insertCourse(String courseName) {
        String names = "";
        int result;
        List<Course> courses = this.selectAll(courseName);
        for (Course course : courses) {
            names = course.getCourseName();
        }
        if (names.equals(courseName)) {
            result = 0;
        } else {
            CourseDao mapper = MybatisUtils.getMapper(CourseDao.class);
            Course course = new Course();
            course.setCourseName(courseName);
            result = mapper.insert(course);
            MybatisUtils.commit();
        }

        return result;
    }

    //查询学生
    public List<Student> selectStudent(String name) {
        Student student = new Student();
        student.setName(name);
        GradeDao mapper = MybatisUtils.getMapper(GradeDao.class);
        return mapper.selectStudentById(student);
    }

    //插入学生
    public int insertStudent(Student student) {
        GradeDao mapper = MybatisUtils.getMapper(GradeDao.class);
        int inserted = mapper.insertStudent(student);
        MybatisUtils.commit();
        return inserted;
    }

    //插入成绩
    public int insertScore(String StudentId, Integer courseId, Double usuallyScore, Double examinationScore) {
        GradeDao mapper = MybatisUtils.getMapper(GradeDao.class);
        double finalScore = usuallyScore * 0.5 + examinationScore * 0.5;
        Score score = new Score();
        score.setStudentId(StudentId);
        score.setCourseId(courseId);
        score.setScores(finalScore);
        int inserted = mapper.insertGrade(score);
        MybatisUtils.commit();
        return inserted;
    }
    //更新成绩
    public int updateScore(String StudentId, Double usuallyScore, Double examinationScore,Integer courseId) {
        GradeDao mapper = MybatisUtils.getMapper(GradeDao.class);
        double finalScore = usuallyScore * 0.5 + examinationScore * 0.5;
        Score score = new Score();
        score.setStudentId(StudentId);
        score.setScores(finalScore);
        score.setCourseId(courseId);
        int updated = mapper.updateStudentGrade(score);
        MybatisUtils.commit();
        return updated;
    }
    //查询课程接口
    public List<Course> selectAll(String courseName) {
        CourseDao mapper = MybatisUtils.getMapper(CourseDao.class);
        Course course = new Course();
        course.setCourseName(courseName);
        return mapper.selectAll(course);
    }

    //查询成绩  单个
    public List<Student> selectByGradeOne(String studentId) {
        GradeDao mapper = MybatisUtils.getMapper(GradeDao.class);
        List<Student> students = mapper.selectOneGrade(studentId);
        MybatisUtils.commit();
        return students;
    }

    //查询单个学生的单条成绩
    public List<Student> selectTwoGrade(String studentId,int courseId) {
        System.out.println(studentId+courseId);
        GradeDao mapper = MybatisUtils.getMapper(GradeDao.class);
        List<Student> list = mapper.selectTwoGrade(studentId,courseId);
        return list;
    }

    //查询成绩  全部
    public List<Student> selectByGradeAll() {
        GradeDao gradeDao = MybatisUtils.getMapper(GradeDao.class);
        return gradeDao.selectAllGrade();
    }

    //学生端分页
    public List<Student> selectScoreByPageStudent(Score score) {
        PageHelper.startPage(score.getPageNumber(), score.getPageSize());
        GradeDao mapper = MybatisUtils.getMapper(GradeDao.class);
        PageInfo<Student> pageInfo = new PageInfo<>(mapper.selectStudentGrade(score));
        return pageInfo.getList();
    }
    //学生分页
    public List<Student> selectByGradeStudent(Integer pageNumber, Integer pageSize,String studentId) {
        Score score = new Score();
        score.setPageNumber(pageNumber);
        score.setPageSize(pageSize);
        score.setStudentId(studentId);
        return selectScoreByPageStudent(score);
    }

    //分页     查询
    public List<Student> selectScoreByPage(Score score) {
        PageHelper.startPage(score.getPageNumber(), score.getPageSize());
        GradeDao mapper = MybatisUtils.getMapper(GradeDao.class);
        PageInfo<Student> pageInfo = new PageInfo<>(mapper.selectAllGrade1(score));
        return pageInfo.getList();
    }
    //返回分页页码
    public int pageNumber(Integer pageNumber, Integer pageSize) {
        Score score = new Score();
        score.setPageNumber(pageNumber);
        score.setPageSize(pageSize);
        PageHelper.startPage(score.getPageNumber(), score.getPageSize());
        GradeDao mapper = MybatisUtils.getMapper(GradeDao.class);
        PageInfo<Student> pageInfo = new PageInfo<>(mapper.selectAllGrade1(score));
        return pageInfo.getNextPage();
    }
    //分页
    public List<Student> selectByGradeAll1(Integer pageNumber, Integer pageSize) {
        Score score = new Score();
        score.setPageNumber(pageNumber);
        score.setPageSize(pageSize);
        return selectScoreByPage(score);
    }

    //删除学生
    public int deleteByStudents(String studentId) {
        int result;
        GradeDao mapper = MybatisUtils.getMapper(GradeDao.class);
        int i = mapper.deleteStudent(studentId);
        MybatisUtils.commit();
        if (i > 0) {
            result = 1;
        } else {
            result = 0;
        }
        return result;
    }

    //删除学生关联的成绩
    public int deleteByStudent(String studentId, Integer courseId) {
        int result;
        GradeDao mapper = MybatisUtils.getMapper(GradeDao.class);
        int a = mapper.deleteGrade(courseId, studentId);
        MybatisUtils.commit();
        if (a > 0) {
            result = 1;
        } else {
            result = 0;
        }

        return result;
    }

    //统一添加
    public int insertByDate(String name, String studentId, String courseName, Double usuallyScore, Double examinationScore) {
        String studentName = "";
        String id = "";
        int result;
        Student student = new Student();
        student.setStudentId(studentId);
        student.setName(name);
        //查询课程id
        List<Course> courses = this.selectAll(courseName);
        int courseId = 0;
        for (Course course : courses) {
            courseId = course.getId();
        }
        //查询学生是否重复
        List<Student> students = this.selectStudent(name);
        for (Student s : students) {
            studentName = s.getName();
            id = s.getStudentId();
        }
        if (studentName.equals(name) && id.equals(studentId)) {
            //插入成绩
            int s = this.insertScore(studentId, courseId, usuallyScore, examinationScore);
            if (s > 0) {
                result = 1;
            } else {
                result = 0;
            }
        } else {
            //插入学生
            int c = this.insertStudent(student);
            //插入成绩
            int s = this.insertScore(studentId, courseId, usuallyScore, examinationScore);
            if (s > 0 && c > 0) {
                result = 1;
            } else {
                result = 0;
            }
        }
        return result;
    }
}


